// pages/arList/arList.js
import { inquireCode, activationCode } from "../../server/api/common.js";

Page({
    data: {
        arList: [],
        inputValue: "",
        statusBarHeight: 0, // 状态栏高度
        navBarHeight: 44 // 导航栏高度
    },

    onLoad() {
        this.getStatusBarHeight();
    },

    getStatusBarHeight() {
        const that = this;
        wx.getSystemInfo({
            success: function (res) {
                that.setData({
                    statusBarHeight: res.statusBarHeight,
                    navBarHeight: res.statusBarHeight + 44 // 44为导航栏高度
                });
            }
        });
    },

    onShow() {
        this.initData();
    },

    initData() {
        // 初始化数据
    },

    input(e) {
        this.setData({
            inputValue: e.detail.value
        });
    },

    Scan() {
        const that = this;
        wx.scanCode({
            success(res) {
                console.log('扫描结果：', res);
                // 将扫描结果赋值给输入框
                that.setData({
                    inputValue: res.result
                });
                // 可以直接调用查询接口
                that.inquire();
            },
            fail(err) {
                console.error('扫描失败：', err);
                wx.showToast({
                    title: '扫描失败，请重试',
                    icon: 'none'
                });
            }
        });
        // 扫描二维码逻辑
    },

    activate(e) {
        const item = e.currentTarget.dataset.item;
        console.log(item,"item");
        if (item.codeStatus === 1) {
            wx.showModal({
                content: "您输入的激活码已使用！",
                showCancel: false,
                confirmText: "关闭",
            });
        } else {
            const code = item.code;
            activationCode(code).then(res => {
                if (res.data.code === 0) {
                    wx.showModal({
                        content: "激活成功！",
                        showCancel: false,
                        confirmText: "关闭",
                    });
                }
            });
        }
    },

    inquire() {
        this.setData({
            arList: []
        });

        inquireCode({ code: this.data.inputValue }).then(res => {
            if (res.data.code === 0) {
                this.setData({
                    arList: [res.data.data]
                });
            } else {
                wx.showModal({
                    content: "激活码错误！",
                    showCancel: false,
                    confirmText: "关闭",
                });
            }
        });
    },

    autoBack() {
        wx.navigateBack({ delta: 1 });
    }
});